<?php
namespace backend\modules\api\controllers;

use Yii;
use yii\db\Query;
use common\utils\FileUpload;

class CommonController extends CoreController
{
	//图片上传
	public function actionUpload() 
	{
		$this->fileUpload();
	}

	//本地上传
	private function fileUpload()
	{
		$basePath = $_SERVER['DOCUMENT_ROOT'];
		$filePath = $this->getFilePath($this->request('uptype'));
		$filePath = '/data/'.$filePath; 	
		$upload = new FileUpload();
		$res = $upload->upload('file',$basePath.$filePath); 
		if($res){
			$fileName = $upload->getFileName();
			$data = array('url'=>'http://'.$_SERVER['HTTP_HOST'].$filePath.'/'.$fileName);
			$this->out('上传成功',$data);
		}
		$this->error('上传失败'.$upload->getErrorMsg());		
	}


	//图片存放路径
	private function getFilePath($upType)
	{
		switch ($upType) {
			case '1':
				$filePath = 'upload/avatar';	
				break;
			
			default:
				$filePath = 'upload/avatar';	
				break;
		}
		return $filePath;
			
	}



}

